package com.gable.report.service.impl;

import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.dao.IChartsForHospitalMaintenanceDao;
import com.gable.report.model.ChartsForMaintenance;
import com.gable.report.model.ChartsHospitalQuery;
import com.gable.report.service.ChartsForHospitalMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gable.report.util.ChartsUtil.*;


/**
 * 医院维护报表
 * Created by mj on 2017/7/27.
 */
@Service
public class ChartsForHospitalMaintenanceServiceImpl implements ChartsForHospitalMaintenanceService {

    @Autowired
    IChartsForHospitalMaintenanceDao chartsForHospitalMaintenanceDao;

    @Override
    public ReturnResultUtil queryDepartmentAssetsForMaintenance(Long hospitalId,ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<ChartsForMaintenance> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDepartmentForUpkeep(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDepartmentForInspection(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDepartmentForCompulsory(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                map.put("list",listForPage(chartsForMaintenances,chartsHospitalQuery));
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryDeviceTypeSubAssetsForMaintenance(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<ChartsForMaintenance> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDeviceTypeForUpkeep(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDeviceTypeForInspection(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryDeviceTypeForCompulsory(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                map.put("list",listForPage(chartsForMaintenances,chartsHospitalQuery));
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryManufactureAssetsForMaintenance(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<ChartsForMaintenance> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryManufactureForUpkeep(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryManufactureForInspection(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryManufactureForCompulsory(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                map.put("list",listForPage(chartsForMaintenances,chartsHospitalQuery));
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryModelAssetsForMaintenance(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<ChartsForMaintenance> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryModelForUpkeep(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryModelForInspection(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryModelForCompulsory(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                map.put("list",listForPage(chartsForMaintenances,chartsHospitalQuery));
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryAssets(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<ChartsForMaintenance> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备

            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryUpkeepAssets(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryInspectionAssets(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryCompulsoryAssets(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                map.put("list",listForPage(chartsForMaintenances,chartsHospitalQuery));
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryOverview(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            Map<String, Object> inspectionOverview = chartsForHospitalMaintenanceDao.queryInspectionOverview(paramMap);
            Map<String, Object> upkeepOverview = chartsForHospitalMaintenanceDao.queryUpkeepOverview(paramMap);
            Map<String, Object> compulsoryOverview = chartsForHospitalMaintenanceDao.queryCompulsoryOverview(paramMap);

            Map<String,Object> returnMap = new HashMap<String,Object>();
            returnMap.put("inspectionOverview",inspectionOverview);
            returnMap.put("upkeepOverview",upkeepOverview);
            returnMap.put("compulsoryOverview",compulsoryOverview);
            return new ReturnResultUtil(1,"success",returnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
    }

    @Override
    public ReturnResultUtil queryForMonth(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryForMonthWithUpkeep(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryForMonthWithInspection(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryForMonthWithCompulsory(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("list",chartsForMaintenances);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryInspectionData(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances = chartsForHospitalMaintenanceDao.queryInspectionData(paramMap);
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                List<Map<String,Object>> returnList = listForPageWithOutOrder(chartsForMaintenances,chartsHospitalQuery);
                map.put("list",returnList);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryCompulsoryData(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances = chartsForHospitalMaintenanceDao.queryCompulsoryData(paramMap);
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                List<Map<String,Object>> returnList = listForPageWithOutOrder(chartsForMaintenances,chartsHospitalQuery);
                map.put("list",returnList);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryUpkeepData(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances = chartsForHospitalMaintenanceDao.queryUpkeepData(paramMap);
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("count",chartsForMaintenances.size());
                List<Map<String,Object>> returnList = listForPageWithOutOrder(chartsForMaintenances,chartsHospitalQuery);
                map.put("list",returnList);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryPlanAssets(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanUpkeepAssets(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanInspectionAssets(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanCompulsoryAssets(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("list",chartsForMaintenances);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryActualAssets(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryActualUpkeepAssets(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryActualInspectionAssets(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryActualCompulsoryAssets(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("list",chartsForMaintenances);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }

    @Override
    public ReturnResultUtil queryPlanRecords(Long hospitalId, ChartsHospitalQuery chartsHospitalQuery) {
        try {
            Map<String,Object> paramMap = getParamMap(hospitalId, chartsHospitalQuery);
            List<Map<String,Object>> chartsForMaintenances;
            //根据前端参数分别查询对应维护的设备
            if(chartsHospitalQuery.getMaintenanceType() == 1){
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanUpkeepRecords(paramMap);
            } else if (chartsHospitalQuery.getMaintenanceType() == 2) {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanInspectionRecords(paramMap);
            } else {
                chartsForMaintenances = chartsForHospitalMaintenanceDao.queryPlanCompulsoryRecords(paramMap);
            }
            if(!CollectionUtils.isEmpty(chartsForMaintenances)){
                Map<String,Object> map = new HashMap<String,Object>();
                map.put("list",chartsForMaintenances);
                return new ReturnResultUtil(1,"success",map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResultUtil(0,"查询异常");
        }
        return new ReturnResultUtil(1,"success");
    }
}
